//
//  DrawPictureViewController.m
//  Paoli
//
//  Created by zlb on 15/11/16.
//  Copyright © 2015年 Ins24.com. All rights reserved.
//

#import "DrawPictureViewController.h"
#import "DrawImageView.h"

@interface DrawPictureViewController ()

@property (nonatomic, weak) DrawImageView *imageView;

@end
@implementation DrawPictureViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = RGB_SAME(245);
    
    self.title = @"图片编辑";
    
    DrawImageView * imageV = [[DrawImageView alloc]initWithFrame:[self caculatorImageFrame]];
    imageV.image = self.image;
    self.imageView = imageV;
    [self.view addSubview:imageV];
    [self setNavUI];
}
- (void)setNavUI
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"保存" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn setTitleColor:RGB_SAME(51) forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [btn sizeToFit];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = item;
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 22, 22);
    [leftButton setBackgroundImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    
    [leftButton addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    
    //    self.navigationItem.leftBarButtonItem = leftItem;
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
    
    negativeSpacer.width = -8;
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, leftItem, nil];
}
- (void)leftBtnClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)rightBtnClick
{
    UIImage *image = [self.imageView saveImage];
    [self.delegate DrawPictureViewController:self Image:image];
    [self.navigationController popViewControllerAnimated:YES];
}
- (CGRect)caculatorImageFrame
{
    if (self.image) {
        CGFloat imageH = self.image.size.height * SCREEN_WIDTH/self.image.size.width;
        CGFloat x = 0;
        CGFloat y = (SCREEN_HEIGHT - imageH) / 2;
        return CGRectMake(x, y, SCREEN_WIDTH, imageH);
    }
    return CGRectZero;
}
@end
